-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local class = require 'mc.class'
local singleton = require 'mc.singleton'
local service = require 'spdm.service'

local component_integrity_mdb = class()

function component_integrity_mdb:ctor(responder_collection)
    self.m_responder_collection = responder_collection
    self.m_mdb_integrity = {}
end

function component_integrity_mdb:init()
    self:regist_signals()
end

function component_integrity_mdb:regist_signals()
    self.m_responder_collection.m_integrity_added:on(function(...)
        self:new_integrity_to_mdb(...)
    end)
end

function component_integrity_mdb:new_integrity_to_mdb(info)
    local integrity_obj = service:CreateComponentIntegrity(tostring(info.m_chassis_id), info.m_mdb_name,
        tostring(info.m_id), function(cur_obj)
        cur_obj.Type = info.m_integrity_info.type
        cur_obj.Version = info.m_integrity_info.version
        cur_obj.LastUpdated = info.m_integrity_info.last_updated
        if not info.m_measurement_info then
            cur_obj.Specification = "DMTF"
            cur_obj.SummaryType = "TCB"
            cur_obj.SummaryHashAlgorithm = ""
            cur_obj.SummaryHash = ""
        else
            cur_obj.Specification = info.m_measurement_info.specification
            cur_obj.SummaryType = info.m_measurement_info.summary_type
            cur_obj.SummaryHashAlgorithm = info.m_measurement_info.hash_algo
            cur_obj.SummaryHash = info.m_measurement_info.hash
        end
    end)
    self.m_mdb_integrity[#self.m_mdb_integrity] = integrity_obj
end

return singleton(component_integrity_mdb)